import { Mycloud } from '../../public'; 
Page({
  data: {
    bili:'',
    day: 0,
    hour: 0,
    min: 0,
    sec: 0,
    voteCode:1,
    showPop:false,//遮罩控制
    voteSuccessPop:false,//投票成功
    voteNoCountPop:false,//没有投票次数了
    tasteList:"",
    offList:"",
    diyList:"",
    tasteNoCountPop:false,
    offNoCountPop:false,
    diyNoCountPop:false,
    userInfoList:"",
    searchValue:"",
    tasteNum:1,
    offNum:1,
    diyNum:1,
    searchSomeShow:false,
    pageList:""
  },

  changeSearchValue(e){
    console.log(e.detail.value)
    this.setData({
      searchValue:e.detail.value
    })
  },

  // 搜索
  searchList(e){
    let  options
    if(e.target.dataset.type=="some"){
      if(this.data.searchValue==""){
        my.showToast({content:"请输入正确的淘宝id进行搜索~"})
        return false
      }
       options = {
        type:3,
        name:this.data.searchValue,
        pageNum:1,
        pageSize:10
      }
      Mycloud('client',options,'getLeaderboardList').then((res)=>{
        if(res.code==0){
           this.setData({
             diyList:res.data.list,
             searchSomeShow:true
           })
         }
      })
    }else{
      options = {
        type:3,
        name:"",
        pageNum:1,
        pageSize:10
      }
      Mycloud('client',options,'getLeaderboardList').then((res)=>{
        if(res.code==0){
           this.setData({
             diyList:res.data.list,
              searchSomeShow:false
           })
         }
      })
    }
  },

  // 翻页
  changePage(e){
    if(e.target.dataset.todo=="prev"){
      this.setData({
        diyNum:this.data.diyNum-1<=0?1:this.data.diyNum-1
      },()=>{
        this.getDiyVoteList()
      })
    }else{
        this.setData({
          diyNum:this.data.diyNum+1>=this.data.pageCount?this.data.pageCount:this.data.diyNum+1
        },()=>{
          this.getDiyVoteList()
        })
    }
  },

  onLoad() {
      this.setData({
      bili:my.getSystemInfoSync().windowWidth/720
    })
    this.getTasteVoteList()
  },
  onShow(){
    this.rankCountdown()
    this.getUserInfoc()
  },

    // 获取用户信息
  async getUserInfoc(){
     let res = await Mycloud('client',{},'getUserInfo');
     console.log("个人信息",res)
     if(res.code==0){
        this.setData({
          userInfoList:res.data
        })
     }else{
       my.showToast({
         content:"网络错误，请重试"
       });
     }
     console.log(res)
     
  },

    // 排行榜 口味
  async getTasteVoteList(){
    const options = {
      type:1,
      pageNum:this.data.tasteNum,
      pageSize:10
    }
     let res = await Mycloud('client',options,'getLeaderboardList');
     if(res.code==0){
       this.setData({
         tasteList:res.data.list,
         searchSomeShow:false
       })
     }
     console.log("口味排行榜",res)
     
  },
  // 排行榜 文案
  async getOffVoteList(){
    const options = {
      type:2,
      pageNum:this.data.offNum,
      pageSize:10
    }
     let res = await Mycloud('client',options,'getLeaderboardList');
    if(res.code==0){
       this.setData({
         offList:res.data.list,
         searchSomeShow:false
       })
     }
     console.log("文案排行榜",res)
     
  },
  // 排行榜 原创
  async getDiyVoteList(){
    const options = {
      type:3,
      pageNum:this.data.diyNum,
      pageSize:10
    }
     let res = await Mycloud('client',options,'getLeaderboardList');
    if(res.code==0){
       this.setData({
         diyList:res.data.list,
         searchSomeShow:false,
         pageCount:res.data.pageCount<=0?1:res.data.pageCount
       })
     }
     console.log("原创排行榜",res)
     
  },

  
  //  倒计时
  rankCountdown() {
    // let obj = new Date()
    let time1 = '2021/04/21 23:59:59'
    var date=new Date(time1.replace(/-/g, '/'));
    let time2 = date.getTime()
    console.log(time2)
    let endTime = parseInt(time2)


    my.getServerTime({
      success: (myresult) => {
        let startTime = parseInt(myresult.time)
        let durationTime = parseInt(endTime) - parseInt(startTime)
        setInterval(() => {
          durationTime -= 1000
          if (durationTime <= 0) {
            // clearInterval(durTime)
            return
          }

          // console.log(durationTime)
          const day = parseInt(durationTime / 1000 / 3600 / 24);
          const hour = parseInt((durationTime / 1000 / 3600) % 24);
          const min = parseInt((durationTime / 1000 / 60) % 60);
          const sec = parseInt((durationTime / 1000) % 60);
          //  console.log("天："+day+"时："+hour+"分："+min+"秒："+sec)
          this.setData({
            day: day,
            hour: hour,
            min: min,
            sec: sec
          })

        }, 1000)

      }
    })
  },
  handleChangeCode(e){
    if(e.target.dataset.voteCode==1){
      this.getTasteVoteList()
    }else if(e.target.dataset.voteCode==2){
      this.getOffVoteList()
    }else{
      this.getDiyVoteList()
    }
    this.setData({
      voteCode:e.target.dataset.voteCode
    })

  },
  // 返回首页
  handleToHome(){
    my.navigateTo({ url: `../../pages/index/index` })
    // my.alert({content:"返回首页"});
  },
  // 去抽奖
  goDraw(){
    my.navigateTo({ url: `../../pages/lottery/lottery` })
  },

  // 投票
  async handleVoteToShow(e){
    const options = {
      type:e.target.dataset.voteType,
      _id:e.target.dataset.id
    }
    console.log("投票的传参")
     let res = await Mycloud('client',options,'vote');
     console.log("投票成功返回数据",res)
     if(res.code==0){
      //  投票成功  弹加次数的弹窗
      this.setData({
        voteSuccessPop:true,
        showPop:true
      },()=>{
        this.getUserInfoc()
        this.getTasteVoteList()
        this.getOffVoteList()
        this.getDiyVoteList()
      })
      
     }else if(res.code==-223){
       if(e.target.dataset.voteType==1){
         this.setData({
           tasteNoCountPop:true,
           showPop:true
         })
        //  口味的没有了
       }else if(e.target.dataset.voteType==2){
        // 文案的没有了
        this.setData({
          offNoCountPop:true,
          showPop:true
        })
       }else{
        // 创作的没有了
        this.setData({
          diyNoCountPop:true,
          showPop:true
        })
       }
     }else{
      my.showToast({
        content:"网络错误，请刷新重试~"
      });
     }
    // my.alert({content:"投票"})
    // 投票前先查是否有次数   有次数再调投票接口 投完之后给投票成功弹窗   然后查询一次3个排行榜数据刷新页面

  },
  // 关闭没有次数的弹窗
  closevoteNoCountPop(){
    console.log("关闭")
    this.setData({
      showPop:false,
      tasteNoCountPop:false,
      offNoCountPop:false,
      diyNoCountPop:false,
    })
  },


// 关闭投票成功弹窗
  closevoteSuccessPop(){
    this.setData({
      voteSuccessPop:false,
      showPop:false
    })
  },


});
